﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace FinancialAnalytic.BusinessLogic
{
    /// <summary>
    /// This class provides functions to serialize and deserialize objects
    /// </summary>
    /// <remarks>
    /// Used to deserialize the configurations files and serialize the result
    /// </remarks>
    public class Serializer
    {
        ///<summary>
        ///Serialize an object to string.
        ///</summary>
        ///<param name="obj">The object to serialize</param>
        ///<param name="sns">The namespace to use for the serializations</param>
        ///<remarks></remarks>

        public static string Serialize(object obj, XmlSerializerNamespaces sns)
        {
            XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

            //create a MemoryStream here because just working with memory
            Stream memStream = new MemoryStream();

            //The XmlTextWriter takes a stream and encoding as the constructor

            XmlTextWriter oWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            serializer.Serialize(oWriter, obj, sns);
            oWriter.Flush();

            //Go back to the beginning of the Stream to read its contents
            memStream.Seek(0, System.IO.SeekOrigin.Begin);

            //Return the string
            //Return Text.Encoding.UTF8.GetString(memStream.ToArray())
            StreamReader reader = new StreamReader(memStream, Encoding.UTF8);
            return reader.ReadToEnd();
        }

        ///<summary>
        ///Serialize an object to string
        ///</summary>
        ///<para name="obj"> The object to serialize </param>
        ///<remarks>
        ///</remarks>

        public static string Serialize(object obj)
        {
            XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());

            StringBuilder sb = new StringBuilder();

            TextWriter textWriter = new StringWriter(sb);

            serializer.Serialize(textWriter, obj);
            textWriter.Close();
            return sb.ToString();
        }

        ///<summary>
        /// Deserialize a file to an object
        /// </summary>
        /// <param name="type">The type of the object to be serialize.</param>
        /// <param name="path">The path of the file</param>
        /// <remarks></remarks>
        /// 
        public static object Deserialize(Type type, string path)
        {

            // Error At FormEngineConfig 1,1
            //XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
            //TextReader textReader = new System.IO.StringReader(path);

            //object obj = serializer.Deserialize(textReader);
            //textReader.Close();
            //return obj;

            // Error At FormEngineConfig 44,16
            XmlSerializer serializer = new XmlSerializer(type);
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            XmlReader reader = XmlReader.Create(fs);
            object obj = serializer.Deserialize(reader);
            reader.Close();
            fs.Close();
            fs.Dispose();
            return obj;

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <param name="defaultNamespace"></param>
        public static object DeserializeString(Type type, string defaultNamespace, string strToDeserialize)
        {
            XmlSerializer serializer = new XmlSerializer(type, defaultNamespace);
            TextReader textReader = new StringReader(strToDeserialize);
            object obj = serializer.Deserialize(textReader);
            textReader.Close();
            return obj;
        }

        public static object DeserializeString(Type type, string strToDeserialize)
        {
            XmlSerializer serializer = new XmlSerializer(type);
            TextReader textReader = new StringReader(strToDeserialize);
            object obj = serializer.Deserialize(textReader);
            textReader.Close();
            return obj;
        }

    }
}
